/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuCashConfig;
import com.boxin.ims.modules.jiujiudevice.service.JiuCashConfigService;

/**
 * 押金配置Controller
 * @author sxh
 * @version 2018-08-15
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuCashConfig")
public class JiuCashConfigController extends BaseController {

	private static final Long GLOBAL_CASH_ID = 1L;//押金配置1条
	@Autowired
	private JiuCashConfigService jiuCashConfigService;
	
	@ModelAttribute
	public JiuCashConfig get(@RequestParam(required=false) Long id) {
//		if (id != null){
//			return jiuCashConfigService.get(id);
//		}else{
//			return new JiuCashConfig();
//		}
		return jiuCashConfigService.get(1L);
	}
	
	@RequiresPermissions("jiujiudevice:jiuCashConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuCashConfig jiuCashConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		
        Page<JiuCashConfig> page = jiuCashConfigService.find(new Page<JiuCashConfig>(request, response), jiuCashConfig); 
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuCashConfigList";
	}

	@RequiresPermissions("jiujiudevice:jiuCashConfig:view")
	@RequestMapping(value = "form")
	public String form(JiuCashConfig jiuCashConfig, Model model) {
	
		User user = UserUtils.getUser();
	
		model.addAttribute("jiuCashConfig", jiuCashConfig);
		return "modules/jiujiudevice/jiuCashConfigForm";
	}

	@RequiresPermissions("jiujiudevice:jiuCashConfig:edit")
	@RequestMapping(value = "save")
	public String save(JiuCashConfig jiuCashConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuCashConfig)){
			return form(jiuCashConfig, model);
		}
		JiuCashConfig jiu = jiuCashConfigService.get(GLOBAL_CASH_ID);
		if(jiuCashConfig != null) {
			jiu.setMoney(jiuCashConfig.getMoney());
			jiu.setTitle(jiuCashConfig.getTitle());
			jiu.setCreateTime(new Date());
			jiuCashConfigService.save(jiu);
		}else{
			jiuCashConfigService.save(jiuCashConfig);
		}
		addMessage(redirectAttributes, "保存押金配置'" + jiuCashConfig.getTitle() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuCashConfig/form?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuCashConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuCashConfigService.delete(id);
		addMessage(redirectAttributes, "删除押金配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuCashConfig/?repage";
	}

}
